/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hjt1;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
 *
 * @author julio
 */
public class CarGUI {
    
    Radio radio;
    
    JFrame frame;
    
    JPanel panelMain;
    
    JPanel panelRadio;
        JButton buttonPower;
        JLabel labelSignal;
        JButton buttonSignal;
        JButton buttonChangeFreq;
        
    JPanel panelTunes;
        JRadioButton buttonTune1;JRadioButton buttonTune2;JRadioButton buttonTune3;
        JRadioButton buttonTune4;JRadioButton buttonTune5;JRadioButton buttonTune6;
        JRadioButton buttonTune7;JRadioButton buttonTune8;JRadioButton buttonTune9;
        JRadioButton buttonTune10;JRadioButton buttonTune11;JRadioButton buttonTune12;
        ButtonGroup group;
        
        JRadioButton selectedRadioButton;
        
        JButton buttonSaveTune;
        int tuneSelected;
       
    public CarGUI(){
        
        radio = new Radio();
        
        tuneSelected = 0;
        
        FlowLayout fL = new FlowLayout();
	BorderLayout bL = new BorderLayout();
        
        panelMain = new JPanel();
        panelMain.setLayout(bL);
        
        panelRadio = new JPanel();
            buttonPower = new JButton("ON");buttonPower.setName("power");
            labelSignal = new JLabel(String.valueOf(radio.get_Freq()));
            buttonSignal = new JButton("FM");buttonSignal.setName("changeSignal");
            buttonChangeFreq = new JButton("+");buttonChangeFreq.setName("changeFreq");
        panelRadio.setLayout(fL);
        panelRadio.add(buttonPower);
        panelRadio.add(labelSignal);
        panelRadio.add(buttonSignal);
        panelRadio.add(buttonChangeFreq);
            
        panelTunes = new JPanel();
            buttonTune1 = new JRadioButton("");buttonTune1.setName("0");
            buttonTune2 = new JRadioButton("");buttonTune2.setName("1");
            buttonTune3 = new JRadioButton("");buttonTune3.setName("2");
            buttonTune4 = new JRadioButton("");buttonTune4.setName("3");
            buttonTune5 = new JRadioButton("");buttonTune5.setName("4");
            buttonTune6 = new JRadioButton("");buttonTune6.setName("5");
            buttonTune7 = new JRadioButton("");buttonTune7.setName("6");
            buttonTune8 = new JRadioButton("");buttonTune8.setName("7");
            buttonTune9 = new JRadioButton("");buttonTune9.setName("8");
            buttonTune10 = new JRadioButton("");buttonTune10.setName("9");
            buttonTune11 = new JRadioButton("");buttonTune11.setName("10");
            buttonTune12 = new JRadioButton("");buttonTune12.setName("11");
            buttonSaveTune = new JButton("Save");buttonSaveTune.setName("saveTune");
        panelTunes.setLayout(fL);
        
        group = new ButtonGroup();
        group.add(buttonTune1);group.add(buttonTune2);group.add(buttonTune3);
        group.add(buttonTune4);group.add(buttonTune5);group.add(buttonTune6);
        group.add(buttonTune7);group.add(buttonTune8);group.add(buttonTune9);
        group.add(buttonTune10);group.add(buttonTune11);group.add(buttonTune12);
        
        panelTunes.add(buttonTune1);panelTunes.add(buttonTune2);panelTunes.add(buttonTune3);
        panelTunes.add(buttonTune4);panelTunes.add(buttonTune5);panelTunes.add(buttonTune6);
        panelTunes.add(buttonTune7);panelTunes.add(buttonTune8);panelTunes.add(buttonTune9);
        panelTunes.add(buttonTune10);panelTunes.add(buttonTune11);panelTunes.add(buttonTune12);
        panelTunes.add(buttonSaveTune);
        
        panelMain.add(panelRadio,"North");
        panelMain.add(panelTunes,"South");
        
        frame = new JFrame("Car");
	frame.setContentPane(panelMain);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setBounds(375,150,450,100);
	frame.setResizable(false);
	frame.setVisible(true);
        
        ButtonEngine bEng = new ButtonEngine();
        buttonPower.addActionListener(bEng);
        buttonSignal.addActionListener(bEng);
        buttonChangeFreq.addActionListener(bEng);
        buttonSaveTune.addActionListener(bEng);
        buttonTune1.addActionListener(bEng);
        buttonTune2.addActionListener(bEng);
        buttonTune3.addActionListener(bEng);
        buttonTune4.addActionListener(bEng);
        buttonTune5.addActionListener(bEng);
        buttonTune6.addActionListener(bEng);
        buttonTune7.addActionListener(bEng);
        buttonTune8.addActionListener(bEng);
        buttonTune9.addActionListener(bEng);
        buttonTune10.addActionListener(bEng);
        buttonTune11.addActionListener(bEng);
        buttonTune12.addActionListener(bEng);
    }
    
    public static void main(String[] Args){
        new CarGUI();
    }
    
    public void logicButtons(JButton clickedButton){
        String btnName = clickedButton.getName();

        if(btnName.compareTo("power")==0){
            boolean power = radio.power();
            turnOnOffPower(power);
        }else if(btnName.compareTo("changeSignal")==0){
            radio.ChangeSignal();
            changeSignalBtn();
        }else if(btnName.compareTo("changeFreq")==0){
            labelSignal.setText(String.valueOf(radio.NextFreq()));
            changeSignalBtn();
        }else if(btnName.compareTo("saveTune")==0){
            radio.SaveFreq(tuneSelected);
            selectedRadioButton.setText(String.valueOf(radio.changeTune(tuneSelected)));
        }
    }
    
    public void logicRadioButton(JRadioButton radioButton){
        tuneSelected = Integer.parseInt(radioButton.getName());
        selectedRadioButton = radioButton;
        if(!radioButton.getText().equals("")){
            labelSignal.setText(String.valueOf(radio.changeTune(tuneSelected)));
        }
    }
    
    public class ButtonEngine implements ActionListener{

	public void actionPerformed(ActionEvent e) {
            if(e.getSource().getClass().getName().equals("javax.swing.JButton")){
		JButton clickedButton = (JButton) e.getSource();
		logicButtons(clickedButton);	
            }else{
                JRadioButton radioButton = (JRadioButton) e.getSource();
                logicRadioButton(radioButton);
            }
	}
		
    }
    
    public void changeSignalBtn(){
        if(radio.IsAm()){
            buttonSignal.setText("AM");
        }else{
            buttonSignal.setText("FM");
        }
    }
    
    public void turnOnOffPower(boolean power){
        if(power){
            buttonPower.setText("ON");
            labelSignal.setEnabled(true);
            buttonSignal.setEnabled(true);
            buttonChangeFreq.setEnabled(true);
            buttonSaveTune.setEnabled(true);
        }else{
            buttonPower.setText("OFF");
            labelSignal.setEnabled(false);
            buttonSignal.setEnabled(false);
            buttonChangeFreq.setEnabled(false);
            buttonSaveTune.setEnabled(false);
        }
    }
}
